<template>
	<loading-wait v-if="loading" ></loading-wait>
	<view class="uni-global-box" >
		<view class="uni-global-title">
			技师管理 / 技师列表
		</view>
		<view class="body">
			<view class="class-manage-top">
				<view class="">
					<u-button type="primary" size="mini" @click="goUrl('./add')" >
						<text class="iconfont icon-shuliang-zengjia"></text>
						<text> &nbsp;新增技师</text>
					</u-button>
				</view>
			</view>
			<u-table class="u-table-senior" >
				<u-tr style="margin-right:15px;" >
					<u-th class="u-table-th"> 技师头像 </u-th>
					<u-th class="u-table-th">技师姓名</u-th>
					<u-th class="u-table-th">技师手机号</u-th>
					<u-th class="u-table-th">所在城市</u-th>
					<u-th class="u-table-th">技师单量</u-th>
					<u-th class="u-table-th">技师余额</u-th>
					<u-th class="u-table-th" width="70px"  >
						技师状态
					</u-th>
					<u-th class="u-table-th">注册时间</u-th>
					<u-th  width="160px" >操作</u-th>
				</u-tr>
				<view class="u-table-senior-body" style="height:65vh" >
					<loaders  v-if="data.length == 0" ></loaders>
					<u-tr  v-for="(item,index) in data" :key="index"
						class="u-table-tr"
					>
						<u-td class="u-table-td" >
							<view class="flex-layout">
								<u-image width="40px" height="40px" :src="item.head_image"
									@click="eventImage(item.head_image)"></u-image>
							</view>
						</u-td>
						<u-td class="u-table-td">{{item.name}}</u-td>
						<u-td class="u-table-td">{{item.mobile || '暂未设置' }}</u-td>
						<u-td class="u-table-td">
							{{item.location.title}}
						</u-td>
						<u-td class="u-table-td">
							{{item.orders}}
						</u-td>
						<u-td class="u-table-td">
							￥{{(item.money *1).toFixed(2)}}
						</u-td>
						<u-td class="u-table-td" width="70px" :style="!item.status.value?'':'color:red'" >
							<text :class="!item.status.value?'uni-color-success':'uni-color-error'">
								{{item.status.text}}
							</text>
						</u-td>
						<u-td class="u-table-td">
							<uni-dateformat :date="item.create_date.time_stamp" ></uni-dateformat>
						</u-td>
						<u-td class="u-table-td " width="160px">
							<view class="u-table-td-util">
								<u-button size="mini" type="primary" @click="update(item._id)" >
									<text class="iconfont icon-xiugai"></text>
									<text>修改</text>
								</u-button>
								<u-button size="mini" type="primary" @click="goUrl('/pages/user/user-list/detail?uid='+item._id)" >
									<text class="iconfont icon-chakan"></text>
									<text>详情</text>
								</u-button>
								<!-- <u-button size="mini" type="error" @click="removeData(item,index)" >
									<text class="iconfont icon-lajitong"></text>
									<text>删除</text>
								</u-button> -->
							</view>
							
						</u-td>
					</u-tr>
				</view>
				<view class="u-table-senior-pagination">
					<uni-pagination @change="enventPages" :total="count"></uni-pagination>
				</view>
			</u-table>
		</view>
	</view>
	<u-toast ref="uToast" />
</template>

<script>
	const global = uniCloud.importObject("global",{customUI:true});
	const db = uniCloud.database();
	const collectionName = 'userinfo' ;
	export default {
		data() {
			return {
				data:[],
				loading:false,
				where:{
					is_technician:1,
					"examine_status.value":1
				},
				// 分页
				limit:10,
				skip:0,
				count:0,
			};
		},
		onLoad() {
			this.getData() ;
		},
		methods:{
			getData(){
				let filed = "mobile,orders,money,create_date,_id,name,head_image,sex,birthday,status,classify,location,id_card,is_technician";
				global.getList(collectionName,this.where,"create_date.time_stamp desc",filed,this.skip,this.limit).then(result => {
					const data = result.data ;
					this.data = data;
					this.data.money =  parseInt(data.money / 100)
					this.count = result.count;
					this.loading = false ;
				})
			},
			// 删除数据
			removeData(item,index){
				const USER_ID = uni.getStorageSync('uni-id-pages-userInfo')._id;
				uni.showModal({title:"您确认删除吗?",content:"数据删除后不可回复,请慎重"}).then(res =>  {
					if(!res.confirm) return ;
					if(!item._id) return ;
					this.loading = true ;
 					let deleteFile = [
						item.head_image?item.head_image:'',
						item.id_card?item.id_card.card_zheng?item.id_card.card_zheng:'' :'',
						item.id_card?item.id_card.card_fan?item.id_card.card_fan:'':''
					];
					global.deleteData(collectionName,{_id:item._id},deleteFile).then(res =>  {
						if(res.affectedDocs == 1){
							this.$refs.uToast.show({
								title:'删除成功',type: 'success',duration:500,position:'top',
								callback:()=>{
									this.data.splice(index,1);
									this.loading = false;
								}
							})
						}else {
							this.loading = false;
							this.$refs.uToast.show({title:result.message,type: 'error',duration:2000,position:'top' })
						}
					})
				})
			},
			// 修改管理员
			update(uid){
				uni.navigateTo({url:'./update?uid='+uid})
			},
			// 分页
			enventPages(val){
				this.loading = true
				this.skip = ( val.current-1) * 10 ;
				this.getData()
			},
			// 图片放大
			eventImage(image) {
				uni.previewImage({
					urls: [image],
				});
			},
			goUrl(url){
				uni.navigateTo({url})
			},
		}
	}
</script>

<style lang="scss">
	.class-manage-top{
		display: flex;
		padding:5px 20px ;
		justify-content:flex-end;
	}
</style>
